package com.codi.bus.core.sys.web.organ.controller;

import com.codi.base.domain.BaseResult;
import com.codi.base.exception.BaseAppException;
import com.codi.bus.core.resultModel.OrganFileResult;
import com.codi.bus.core.service.organ.OrgFileService;
import com.codi.bus.core.sys.domain.OrgFile;
import com.codi.bus.core.sys.service.FundOrganProfileService;
import com.codi.bus.core.sys.service.OrganFileService;
import com.codi.superman.base.manager.FileUploadMgr;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Pageable;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * Created by wangzhenhao on 2017/9/14.
 */

/**
 * 后台管理 机构文件管理
 */
@RestController
@RequestMapping("/sys/organ")
public class OrganFilesController {
    @Autowired
    private FundOrganProfileService profileService;

    @Autowired
    private OrganFileService organFileService;

    @Autowired
    private OrgFileService orgFileService;

    @Autowired
    private FileUploadMgr fileUploadMgr;

    @RequestMapping(value = "get_organ_files", method = RequestMethod.POST)
    public BaseResult getOrganFiles(Long userId) throws BaseAppException {
        List<OrganFileResult> result = orgFileService.queryFilesByUserId(userId);

        if (result == null) {
            return BaseResult.success(result);
        }

        for (int i = 0; i < result.size(); i++) {
            System.out.println(result.get(i).getId());
            Long fileId = result.get(i).getId();
            String fileUrl = fileUploadMgr.generatePrivUrl(result.get(i).getFileUrl());
            result.get(i).setFileUrl(fileUrl);
        }

        return BaseResult.success(result);
    }

    /**
     * 分页获取文件资源
     */
    @RequestMapping(value = "files", method = RequestMethod.GET)
    public BaseResult queryFiles(Pageable pageReq) throws BaseAppException {
        if (pageReq == null) {
            return new BaseResult(false);
        }

        return organFileService.queryList(pageReq);
    }

    /**
     * 分页获取 加入搜索
     */
    @RequestMapping(value = "files_search", method = RequestMethod.GET)
    public BaseResult queryFiles(Pageable pageReq, String content) throws BaseAppException {
        if (pageReq == null) {
            return new BaseResult(false);
        }

        return organFileService.search(pageReq, content);
    }


    /**
     * 分页获取机构信息
     */
    @RequestMapping(value = "list_info", method = RequestMethod.GET)
    public BaseResult list(Pageable pageReq) {
        if (pageReq == null) {
            return new BaseResult(false);
        }

        return profileService.queryList(pageReq);
    }

    @RequestMapping(value = "list_info_search", method = RequestMethod.GET)
    public BaseResult listSearch(Pageable pageReq, String content) {
        if (pageReq == null || content == null) {
            return new BaseResult(false);
        }

        return profileService.search(pageReq, content);
    }

    @RequestMapping(value = "/{fileId}", method = RequestMethod.GET)
    public BaseResult getFileInfoByFileId(@PathVariable(name = "fileId") Long fileId) {
        return organFileService.getFileInfoByFileId(fileId);
    }

    @RequestMapping(value = "save_file", method = RequestMethod.POST)
    public BaseResult saveFileInfo(OrgFile orgFile) {
        return organFileService.updateFileInfo(orgFile);
    }
}
